﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SMS.Data;

namespace Wicresoft.SMS.Web.Controllers
{
    public class TemplateController : Controller
    {
        //
        // GET: /Template/

        public ActionResult Index(TemplateModel model)
        {
            if (model.page == 0)
                model.page = 1;
            if (model.key == null)
                model.key = "";
            WebConfigProvider csp = new WebConfigProvider();
            int max = Convert.ToInt32(csp.MaxshowInHistory);
            ITemplateService ts = new TemplateDataAccess(csp);
            model.list = ts.GetTemplatesByUserID(2, model.page, max, model.key);
            model.maxpages = ts.GetPageCount(2, max, model.key);
            return View(model);
        }

        //[HttpPost]
        public ViewResult DeleteTemplate(int id)
        {
            WebConfigProvider csp = new WebConfigProvider();
            ITemplateService ts = new TemplateDataAccess(csp);
            ts.DeleteTemplateByID(id);

            TemplateModel model = new TemplateModel();
            if (model.page == 0)
                model.page = 1;
            if (model.key == null)
                model.key = "";
            int max = Convert.ToInt32(csp.MaxshowInHistory);
            //TODO需要动态绑定UserID
            model.list = ts.GetTemplatesByUserID(2, model.page, max, model.key);
            model.maxpages = ts.GetPageCount(2, max, model.key);
            return View("historygrid", model);
        }

        public ViewResult DeleteTemplates(string[] items)
        {
            WebConfigProvider configProvider = new WebConfigProvider();
            ITemplateService dataAccess = new TemplateDataAccess(configProvider);
            if (items != null && items.Length != 0)
            {
                foreach (var id in items)
                {
                    dataAccess.DeleteTemplateByID(Convert.ToInt32(id));
                }
            }
            TemplateModel model = new TemplateModel();
            if (model.page == 0)
                model.page = 1;
            if (model.key == null)
                model.key = "";
            int max = Convert.ToInt32(configProvider.MaxshowInHistory);
            //TODO需要动态绑定UserID
            model.list = dataAccess.GetTemplatesByUserID(2, model.page, max, model.key);
            model.maxpages = dataAccess.GetPageCount(2, max, model.key);
            return View("historygrid", model);
        }
    }
}
